package com.bash.carrep.ui;

import com.bash.carrep.dao.enties.CreditLevel;
import com.bash.carrep.dao.enties.Login;
import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.Terminal;
import com.vaadin.terminal.ThemeResource;

import com.vaadin.ui.*;
import com.vaadin.ui.Window.Notification;

/**
 * 
 * @author bash
 */
public class CarrepApplication extends Application implements
        ItemClickListener, ValueChangeListener {

    Window loginWin;
    private VerticalLayout body;
    Login l;
    MasterDetailSplit masterDetail;
    HorizontalSplitPanel contentpane = new HorizontalSplitPanel();
    private NavigationTree tree;
    private String bannerParh = "";

    @Override
    public void init() {

        setTheme("carrep");
        Window mainWindow = new Window("Carrep Application");
        setMainWindow(mainWindow);
        getMainWindow().addWindow(createLoginWindow());

    }

    private void buildMainLayout() {

        l = (Login) getUser();

        body = new VerticalLayout();
        body.setSizeFull();

        tree = new NavigationTree(this);
        //createBanner here
       // createBanner();
        body.addComponent(createToolbar(l.getCreditLevel()));
        contentpane.setSplitPosition(200, HorizontalSplitPanel.UNITS_PIXELS);
        contentpane.setFirstComponent(tree);

        // contentpane.setSecondComponent(new CreditLevelTable(this));

        body.addComponent(contentpane);
        body.setExpandRatio(contentpane, 1);
        getMainWindow().setContent(body);
    }

    private HorizontalLayout createToolbar(CreditLevel cl) {

        HorizontalLayout hl = null;

        switch (cl.getIdcreditLevel()) {
            case 1:
                hl = new ToolbarAdmin(this);

                break;
        }
        hl.setStyleName("toolbar");
        return hl;
    }

    void enter() {
        buildMainLayout();
        getMainWindow().removeWindow(loginWin);
        showRightView();
    }

    private void setCurrentView(Component c) {

        contentpane.setSecondComponent(c);
    }

    private Component createBanner() {

        Embedded em = new Embedded("caption", new ThemeResource(bannerParh));
        return em;
    }

    private void showRightView() {
        l = (Login) getUser();
        int cl = l.getCreditLevel().getIdcreditLevel();

        switch (cl) {
            case 1:
                showCreditLevelView();
                break;
        }
    }

    Window createLoginWindow() {

        loginWin = new Window("Carrep-Authentification");

        Panel pan = new Panel("Connexion");
        LoginForm lf = new LoginForm(this);
        pan.addComponent(lf);

        loginWin.addComponent(pan);
        loginWin.center();
        loginWin.setModal(true);
        loginWin.setClosable(false);
        loginWin.setWidth("500");
        loginWin.setHeight("300");

        return loginWin;
    }

    Component getCreditLevelView() {
        masterDetail = new MasterDetailSplit(new CreditLevelTable(this), new CreditLevelForm(this));
        return masterDetail;
    }

 
    void showCreditLevelView() {
        setCurrentView(getCreditLevelView());
    }
    
    
  @Override
    public void itemClick(ItemClickEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public void terminalError(Terminal.ErrorEvent event) {

        getMainWindow().showNotification(
                "An internal error has occurred, please "
                + "contact the administrator!",
                Notification.TYPE_ERROR_MESSAGE);

        System.err.println(event.getThrowable());

    }

    @Override
    public void valueChange(ValueChangeEvent event) {

        Property property = event.getProperty();
        if (property == masterDetail.getTable()) {
            Item item = masterDetail.getTable().getItem(masterDetail.getTable().getValue());
            if (item != masterDetail.getForm().getItemDataSource()) {
                masterDetail.getForm().setItemDataSource(item);
            }
        }
    }

    void showUsersView() {
        setCurrentView(getUsersView());
    }

    private Component getUsersView() {
        masterDetail = new MasterDetailSplit(new UserTable(this), new UserForm(this));
        return masterDetail;
    }
    
    

    Component getTyPePMView() {
       masterDetail = new MasterDetailSplit(new CategPmTable(this), new CategPMFom(this));
        return masterDetail;
    }

    void showTyPePMView() {
        setCurrentView(getTyPePMView());
    }
}
